import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query } from '@nestjs/common'
import { ApiOperation, ApiParam, ApiResponse, ApiTags } from '@nestjs/swagger'
import { ApiSecurityAuth } from '~/common/decorators/swagger.decorator'
import { definePermission, Perm } from '~/modules/admin/auth/decorators/permission.decorator'

import { CreateBookDto } from '../dto/create-book.dto'
import { QueryBookDto } from '../dto/query-book.dto'
import { UpdateBookDto } from '../dto/update-book.dto'
import { BookService } from '../services/book.service'

export const permissions = definePermission('study:book', {
  LIST: 'list',
  CREATE: 'create',
  READ: 'read',
  UPDATE: 'update',
  DELETE: 'delete',
} as const)

@ApiTags('Study - 小书屋管理')
@ApiSecurityAuth()
@Controller('admin/study/book')
export class BookController {
  constructor(private readonly bookService: BookService) {}

  @ApiOperation({ summary: '创建书籍' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @Perm(permissions.CREATE)
  @Post()
  create(@Body() createBookDto: CreateBookDto) {
    return this.bookService.create(createBookDto)
  }

  @ApiOperation({ summary: '获取书籍列表（分页）' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.LIST)
  @Get()
  findAll(@Query() query: QueryBookDto) {
    return this.bookService.findAll(query)
  }

  @ApiOperation({ summary: '获取单个书籍' })
  @ApiParam({ name: 'id', description: '书籍ID' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Perm(permissions.READ)
  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.bookService.findOne(+id)
  }

  @ApiOperation({ summary: '更新书籍信息' })
  @ApiParam({ name: 'id', description: '书籍ID' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Perm(permissions.UPDATE)
  @Patch(':id')
  update(@Param('id', ParseIntPipe) id: number, @Body() updateBookDto: UpdateBookDto) {
    return this.bookService.update(+id, updateBookDto)
  }

  @ApiOperation({ summary: '删除书籍' })
  @ApiParam({ name: 'id', description: '书籍ID' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Perm(permissions.DELETE)
  @Delete(':id')
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.bookService.remove(+id)
  }
}
